package com.jysz.certificate.service.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.jysz.certificate.persistence.dao.system.BrandDao;
import com.jysz.certificate.persistence.model.system.Brand;
import com.jysz.webcore.ssh.orm.hibernate.support.Page;
import com.jysz.webcore.ssh.service.EntityService;

@Service
public class BrandService   extends EntityService<Brand ,Long,BrandDao>{

	@Resource
	public BrandDao brandDao;
	@Override
	public BrandDao getEntityDao() {
		return brandDao;
	}
	public Brand selete(Long id){
		Brand brand =brandDao.find(id);
		return brand;
	}
	public List<Brand> seleteAll(){
		return brandDao.findAll();
	}
	public Page<Brand> findPage(int pageNo, int pageSize){
		return this.findPage(brandDao.createCriteria(), pageNo, pageSize);
	}
	public Page<Brand> indexBrand(Brand brand ,int pageNo, int pageSize){
		Criteria criteria = brandDao.createCriteria();
		if(brand!=null){
			if(StringUtils.isNotBlank(brand.getName())){
				criteria.add(Restrictions.like(Brand.COLUMN_NAME, brand.getName(), MatchMode.ANYWHERE));
			}
		}
		return this.findPage(criteria, pageNo, pageSize);
	}
	public void updateThe(){
		Criteria criteria = brandDao.createCriteria();
		criteria.add(Restrictions.eq("theDefault",0));
		List<Brand> list =criteria.list();
		if(list.size()>0){
			for(Brand brand : list){
				brand.setTheDefault(1);
				this.save(brand);
			}
		}
	}
	public List<Brand> detailsBrand(Long[] id){
		Criteria criteria = brandDao.createCriteria();
		criteria.add(Restrictions.in("id", id));
		List<Brand> list =criteria.list();
		return list;
	}
	/*
	 * 查询默认主机厂
	 */
	public Brand seleteTheDefault(){
		Criteria criteria = brandDao.createCriteria();
		criteria.add(Restrictions.eq("theDefault",0));
		List<Brand> list =criteria.list();
		return list.get(0);
	}
	/**
	 * 查询所有的渠道
	 * @param name
	 * @return
	 */
	public Map<String, Brand> brandMap(){
		List<Brand> brands = brandDao.findAll();
		Map<String, Brand> map = new HashMap<String, Brand>();
		for(int i=0;i<brands.size();i++){
			Brand brand = brands.get(i);
			map.put(brand.getName(), brand);
		}
		return map;
	}
	
	public Brand brandName(String name){
		return brandDao.brandName(name);
	}
		

}
